﻿using System;


namespace epicenter2010.Domain.Service
{
	/// <summary>
	/// bstack @ 05/06/2010
	/// Process order service
	/// </summary>
	public class ProcessOrder : epicenter2010.DDD.Domain.Service.IProcessOrder
	{
		private readonly epicenter2010.Domain.Factory.IOrder c_orderFactory;
		private readonly epicenter2010.Domain.Repository.IOrder c_orderRepository;
		private readonly epicenter2010.Domain.Event.IOrderBroker c_eventBroker;


		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="orderFactory"></param>
		/// <param name="orderRepository"></param>
		/// <param name="eventBroker"></param>
		public ProcessOrder(
			epicenter2010.Domain.Factory.IOrder orderFactory,
			epicenter2010.Domain.Repository.IOrder orderRepository,
			epicenter2010.Domain.Event.IOrderBroker eventBroker)
		{
			epicenter2010.Common.DBCChecker.RequireArgumentNotNull("orderFactory", orderFactory);
			epicenter2010.Common.DBCChecker.RequireArgumentNotNull("orderRepository", orderRepository);
			epicenter2010.Common.DBCChecker.RequireArgumentNotNull("eventBroker", eventBroker);

			this.c_orderFactory = orderFactory;
			this.c_orderRepository = orderRepository;
			this.c_eventBroker = eventBroker;
		}


		/// <summary>
		/// Create initial order
		/// </summary>
		/// <param name="subject"></param>
		public void CreateInitialOrder(epicenter2010.Domain.Service.ProcessOrderRequest subject)
		{
			// May do logging here ...(May have ILogger or could be Aspected out)

			epicenter2010.Common.DBCChecker.RequireArgumentNotNull("subject", subject);

			// Get the next order Id
			var _newOrderId = this.c_orderRepository.GetNewOrderId();

			// Creating the order
			var _order = this.c_orderFactory.Create(_newOrderId, subject);

			// Adding the order the database
			this.c_orderRepository.Save(_order);

			// Raise event order created
			this.c_eventBroker.RaiseOrderCreated(_order);

			// May do logging here ...
		}
	}
}
